package zhoufu.sz.sanxiao_check.net;

import android.content.Context;
import android.util.Log;

import com.google.gson.Gson;

import java.util.HashMap;

import javax.inject.Singleton;

import rx.Observable;
import rx.Subscription;
import rx.functions.Func1;
import rx.functions.Func3;
import zhoufu.sz.sanxiao_check.inject.RetrofitQualifier;
import zhoufu.sz.sanxiao_check.models.AddEmptySpaceModel;
import zhoufu.sz.sanxiao_check.models.AddFiveInOneModel;
import zhoufu.sz.sanxiao_check.models.BaseModel;
import zhoufu.sz.sanxiao_check.models.BuildStreetInfo;
import zhoufu.sz.sanxiao_check.models.CheckItemModel;
import zhoufu.sz.sanxiao_check.models.FireDeviceModel;
import zhoufu.sz.sanxiao_check.models.FireNoticeModel;
import zhoufu.sz.sanxiao_check.models.IDCardModel;
import zhoufu.sz.sanxiao_check.models.IDModel;
import zhoufu.sz.sanxiao_check.models.SecureImgEntity;
import zhoufu.sz.sanxiao_check.models.SecureModel;
import zhoufu.sz.sanxiao_check.models.StreetInfoModel;
import zhoufu.sz.sanxiao_check.models.TrainListModel;
import zhoufu.sz.sanxiao_check.models.UpdateStreetInfoModel;
import zhoufu.sz.sanxiao_check.models.User;
import zhoufu.sz.sanxiao_check.utils.DESUtil;

import static zhoufu.sz.sanxiao_check.utils.DESUtil.EncryptAsToMap;

/**
 * User:RCP
 * email:ruancaipu@gmail.com
 * Date:2016/7/26
 */
@Singleton
public final class BasicRequestRepository extends RequestRepository
{
    public BasicRequestRepository(Context Context,
            @RetrofitQualifier Gson gson)
    {
        super(Context, gson);
    }

    public Subscription UpdateStreetInfo(BuildStreetInfo info,
            RequestCallBack<UpdateStreetInfoModel> back)
    {
        if ( back == null )
        {
            throw new NullPointerException("RequestCallBack can't be empty");
        }
        String json = new Gson().toJson(info);
        String map = DESUtil
                .encryptAsDoNet(json);
        Log.e("UpdateStreetInfo", "Json=" + json);
        Observable<String> loginLatest = getStringObservable(map, "UpdateStreetInfo2");
        Subscription subscription = subChainWithCallBack(loginLatest, UpdateStreetInfoModel.class,
                back);
        addSubscribe(subscription);
        return subscription;
    }


    //添加五位一体信息
    public Subscription AddFiveInOne(AddFiveInOneModel info,
            RequestCallBack<BaseModel> back)
    {
        if ( back == null )
        {
            throw new NullPointerException("RequestCallBack can't be empty");
        }
        String map =
                EncryptAsToMap(info);
        Observable<String> loginLatest = getStringObservable(map, "UpdateSupervise");
        Subscription subscription = subChainWithCallBack(loginLatest, BaseModel.class,
                back);
        addSubscribe(subscription);
        return subscription;
    }

    //获取五位一体信息
    public Subscription GetSupervise(String SID,
                                     RequestCallBack<AddFiveInOneModel> back)
    {
        if ( back == null )
        {
            throw new NullPointerException("RequestCallBack can't be empty");
        }
        String map =
                EncryptAsToMap(
                        _mapBuilder.creatationHashMap().SID(SID)
                                .build());
        Observable<String> loginLatest = getStringObservable(map, "GetSupervise");
        Subscription subscription = subChainWithCallBack(loginLatest, AddFiveInOneModel.class,
                back);
        addSubscribe(subscription);
        return subscription;
    }


    public Subscription GetCheckHistory(String SID,
                                     RequestCallBack<Object> back)
    {
        if ( back == null )
        {
            throw new NullPointerException("RequestCallBack can't be empty");
        }
        String map =
                EncryptAsToMap(
                        _mapBuilder.creatationHashMap().SID(SID)
                                .build());
        Observable<String> loginLatest = getStringObservable(map, "GetListExamine");
        Subscription subscription = subChainWithCallBack(loginLatest, Object.class,
                back);
        addSubscribe(subscription);
        return subscription;
    }

    public Subscription GetExamineForPrint(String SID,String checkId,
                                        RequestCallBack<CheckItemModel> back)
    {
        if ( back == null )
        {
            throw new NullPointerException("RequestCallBack can't be empty");
        }
        HashMap<String, Object> build = _mapBuilder.creatationHashMap().SID(SID)
                .build();
        build.put("Id",checkId);
        String map =
                EncryptAsToMap(build);
        Observable<String> loginLatest = getStringObservable(map, "GetExamineForPrint");
        Subscription subscription = subChainWithCallBack(loginLatest, CheckItemModel.class,
                back);
        addSubscribe(subscription);
        return subscription;
    }

    //添加基本信息
    public Subscription AddStreetDetail(FireDeviceModel info,
                                        RequestCallBack<BaseModel> back)
    {
        if ( back == null )
        {
            throw new NullPointerException("RequestCallBack can't be empty");
        }
        String map =
                EncryptAsToMap(info);
        Observable<String> loginLatest = getStringObservable(map, "AddStreetDetail");
        Subscription subscription = subChainWithCallBack(loginLatest, BaseModel.class,
                back);
        addSubscribe(subscription);
        return subscription;
    }

    //获取消防设施配备情况
    public Subscription GetStreetDetail(String SID, RequestCallBack<FireDeviceModel> back)
    {
        if ( back == null )
        {
            throw new NullPointerException("RequestCallBack can't be empty");
        }
        String map =
                EncryptAsToMap(
                        _mapBuilder.creatationHashMap().SID(SID)
                                .build());
        Observable<String> loginLatest = getStringObservable(map, "GetStreetDetail");
        Subscription subscription = subChainWithCallBack(loginLatest, FireDeviceModel.class,
                back);
        addSubscribe(subscription);
        return subscription;
    }


    //获取消防安全告知书
    public Subscription GetAgreement(String SID, RequestCallBack<FireNoticeModel> back)
    {
        if ( back == null )
        {
            throw new NullPointerException("RequestCallBack can't be empty");
        }
        String map =
                EncryptAsToMap(
                        _mapBuilder.creatationHashMap().SID(SID)
                                .build());
        Observable<String> loginLatest = getStringObservable(map, "GetAgreement");
        Subscription subscription = subChainWithCallBack(loginLatest, FireNoticeModel.class,
                back);
        addSubscribe(subscription);
        return subscription;
    }

    //更新消防安全告知书
    public Subscription UpdateAgreement(final String SID,
            String SignPath,
            RequestCallBack<BaseModel> back)
    {
        Observable<String> addAgreementNew = _UpLoadImage(SignPath)
                .switchMap(new Func1<String, Observable<String>>()
                {
                    @Override
                    public Observable<String> call(String s)
                    {
                        String map =
                                EncryptAsToMap(
                                        _mapBuilder.creatationHashMap().SID(SID)
                                                .SignPath(s)
                                                .build());
                        return getStringObservable(map, "UpdateAgreement");
                    }
                });
        return addSubscribe(subChainWithCallBack(addAgreementNew, BaseModel.class, back));
    }

    //获取消防安全告知书
    public Subscription GetLicense(String SID, RequestCallBack<IDCardModel> back)
    {
        if ( back == null )
        {
            throw new NullPointerException("RequestCallBack can't be empty");
        }
        String map =
                EncryptAsToMap(
                        _mapBuilder.creatationHashMap().SID(SID)
                                .build());
        Observable<String> loginLatest = getStringObservable(map, "GetLicense");
        Subscription subscription = subChainWithCallBack(loginLatest, IDCardModel.class,
                back);
        addSubscribe(subscription);
        return subscription;
    }

    //添加消防设施配备情况
    public Subscription UpdateLicense(final IDCardModel info,
            RequestCallBack<BaseModel> back)
    {
        if ( back == null )
        {
            throw new NullPointerException("RequestCallBack can't be empty");
        }
        Observable<String> ob1 = _UploadImageOne(info.License.get());
        Observable<String> ob2 = _UploadImageOne(info.CardTo.get());
        Observable<String> ob3 = _UploadImageOne(info.CardEnd.get());
        Observable<String> zip = Observable
                .zip(ob1, ob2, ob3, new Func3<String, String, String, String>()
                {
                    @Override public String call(String s, String s2, String s3)
                    {
                        info.License.set(s);
                        info.CardTo.set(s2);
                        info.CardEnd.set(s3);
                        String map = DESUtil
                                .EncryptAsToMap(
                                        _mapBuilder.creatationHashMap().SID(info.SID.get())
                                                .License(info.License.get())
                                                .CardTo(info.CardTo.get())
                                                .CardEnd(info.CardEnd.get()).build());
                        return map;
                    }
                }).switchMap(new Func1<String, Observable<? extends String>>()
                {
                    @Override public Observable<? extends String> call(String s)
                    {
                        return getStringObservable(s, "UpdateLicense");
                    }
                });
        Subscription subscription = subChainWithCallBack(zip, BaseModel.class,
                back);
        addSubscribe(subscription);
        return subscription;
    }

    //获取分级评定
    public Subscription GetSecure(String SID, RequestCallBack<SecureModel> back)
    {
        if ( back == null )
        {
            throw new NullPointerException("RequestCallBack can't be empty");
        }
        String map =
                EncryptAsToMap(
                        _mapBuilder.creatationHashMap().SID(SID)
                                .build());
        Observable<String> loginLatest = getStringObservable(map, "GetSecure");
        Subscription subscription = subChainWithCallBack(loginLatest, SecureModel.class,
                back);
        addSubscribe(subscription);
        return subscription;
    }

    //更新分级评定表图片
    public Subscription UpdateSecure(final SecureImgEntity entity, final String SID,
            RequestCallBack<IDModel> callBack)
    {
        Observable<String> addAgreementNew = _UploadImageOne(entity.PhotoLocal.get())
                .switchMap(new Func1<String, Observable<String>>()
                {
                    @Override
                    public Observable<String> call(String s)
                    {
                        entity.PhotoLocal.set(s);
                        String map =
                                EncryptAsToMap(
                                        _mapBuilder.creatationHashMap().SID(SID).PhotoPath(s)
                                                .PhotoID(entity.PhotoID.get())
                                                .PhotoType(entity.getType())
                                                .Describe(entity.Describe.get())
                                                .build());
                        return getStringObservable(map, "UpdateSecure");
                    }
                });
        return addSubscribe(subChainWithCallBack(addAgreementNew, IDModel.class, callBack));
    }

    //场所变更
    public Subscription StreetChange(StreetInfoModel model,
            RequestCallBack<IDModel> back)
    {
        if ( back == null )
        {
            throw new NullPointerException("RequestCallBack can't be empty");
        }
        String map =
                EncryptAsToMap(
                        _mapBuilder.creatationHashMap().SID(model.getSID())
                                .CID(User.readUser().getCharge_ID()).RID(model.getRID())
                                .Name(model.getName()).Type(model.getType())
                                .Business(model.getBusiness()).Has_Business(model.getHas_Business())
                                .Run_Name(model.getRun_Name()).Run_Phone(model.getRun_Phone())
                                .UserID(User.readUser().getID()).build());
        Observable<String> loginLatest = getStringObservable(map, "StreetChange");
        Subscription subscription = subChainWithCallBack(loginLatest, IDModel.class,
                back);
        addSubscribe(subscription);
        return subscription;
    }

    public Subscription GetLastExamine(String SID, RequestCallBack<CheckItemModel> back)
    {
        if ( back == null )
        {
            throw new NullPointerException("RequestCallBack can't be empty");
        }
        String map =
                EncryptAsToMap(
                        _mapBuilder.creatationHashMap().SID(SID)
                                .build());
        Observable<String> loginLatest = getStringObservable(map, "GetLastExamine");
        Subscription subscription = subChainWithCallBack(loginLatest, CheckItemModel.class,
                back);
        addSubscribe(subscription);
        return subscription;
    }


    //获取培训列表
    public Subscription GetStreetTrainByPage(String SID, int PageIndex,
            RequestCallBack<TrainListModel> back)
    {
        if ( back == null )
        {
            throw new NullPointerException("RequestCallBack can't be empty");
        }
        String map =
                EncryptAsToMap(
                        _mapBuilder.creatationHashMap().SID(SID).PageIndex(PageIndex).PageSize()
                                .build());
        Observable<String> loginLatest = getStringObservable(map, "GetStreetTrainByPage");
        Subscription subscription = subChainWithCallBack(loginLatest, TrainListModel.class,
                back);
        addSubscribe(subscription);
        return subscription;
    }

    //新增培训内容
    public Subscription AddStreetTrain(final String SID, final String title, final String content,
            String paths,
            RequestCallBack<BaseModel> callBack)
    {
        Observable<String> getStreetTrainByPage = _UpLoadImage(paths)
                .switchMap(new Func1<String, Observable<String>>()
                {
                    @Override public Observable<String> call(String s)
                    {
                        String map =
                                EncryptAsToMap(
                                        _mapBuilder.creatationHashMap().SID(SID).Title(title)
                                                .Contents(content)
                                                .UserID(User.readUser().getID()).Photo(s)
                                                .build());
                        return getStringObservable(map, "AddStreetTrain");
                    }
                });
        Subscription subscription = subChainWithCallBack(getStreetTrainByPage, BaseModel.class,
                callBack);
        addSubscribe(subscription);
        return subscription;
    }


    //获取检查记录信息(用于手机打印)
    public Subscription GetLastExamineForPrint(String SID, RequestCallBack<CheckItemModel> back)
    {
        if ( back == null )
        {
            throw new NullPointerException("RequestCallBack can't be empty");
        }
        String map =
                EncryptAsToMap(
                        _mapBuilder.creatationHashMap().SID(SID)
                                .build());
        Observable<String> loginLatest = getStringObservable(map, "GetLastExamineForPrint");
        Subscription subscription = subChainWithCallBack(loginLatest, CheckItemModel.class,
                back);
        addSubscribe(subscription);
        return subscription;
    }

    //新建空铺档案
    public Subscription InsertEmptyShopInfo(final AddEmptySpaceModel spaceModel,
            RequestCallBack<IDModel> callBack)
    {
        Observable<String> getStreetTrainByPage = _UploadImageOne(spaceModel.getStorePhoto())
                .switchMap(new Func1<String, Observable<String>>()
                {
                    @Override public Observable<String> call(String s)
                    {
                        spaceModel.setStorePhoto(s);
                        String json = getGson().toJson(spaceModel);
                        return getStringObservable(DESUtil.encryptAsDoNet(json),
                                "InsertEmptyShopInfo");
                    }
                });
        Subscription subscription = subChainWithCallBack(getStreetTrainByPage, IDModel.class,
                callBack);
        addSubscribe(subscription);
        return subscription;
    }
}
